<?php

class UserController extends AppController {

	public $name = 'user';
	
	function beforeFilter() {
	    parent::beforeFilter(); 
		$this->C_auth->autoRedirect = false; //For override login
	    $this->C_auth->allowedActions = array('index','edit','deluser','locked','unlocked','checkuseremail','money');
	}
	
	public function index() {
        $page=Request::get_args("page");	
        $name = Request::get_args('name');
        $email = Request::get_args('email');
        $start_time = Request::get_args('start_time');
        $end_time = Request::get_args('end_time');
        $user_status = Request::get_args('user_status');
        $user_rank_id = Request::get_args('user_rank_id');	

        $conditions = array();		
        //判断/添加用户名查询条件
        if($name) {
		    $conditions['user.username like'] = "%$name%";		
	    }
        //判断/添加email查询条件
        if($email) {
		    $conditions['user.email like'] = "%$email%";		
	    }
        //判断/添加注册时间大于等于开始时间查询条件
        if($start_time) {
		    $conditions['user.reg_time >='] = "$start_time";		
	    }
        //判断/添加注册时间小于等于结束时间查询条件
        if($end_time) {
		    $conditions['user.reg_time <='] = "$end_time";		
	    }

        //判断/添加等级查询条件
        if($user_rank_id) {
		    $conditions['user.rank_id'] = "$user_rank_id";		
	    }

        //判断/添加锁定状态查询条件
        if($user_status!="" && $user_status!=2) {
		    $conditions['user.locked'] = "$user_status";		
	    }
        $url_this = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
        $tb = array('model' => 'user',
 		            'joins' => array(
					    array('type'=>'left','table'=>'shopinfo','conditions'=>array('user.id' =>'shopinfo.user_id')),					
						),
				    'fields' => array('user.*','shopinfo.shop_name'),
					'conditions' => $conditions,
					'order' => ' user.id desc ',
					'limit' => '10',
					'group' => '',
					'url' => preg_replace("/&page=([^&]+)/","",$url_this),
					'page' => $page,
					'plist' => 6				
				); 
	    $result =  $this->fpage($tb); 		
        $userrank =array();		
		$query = array(
			'order' => ' id asc ', 
			'fields' => array('userrank.*'), 
		);
		$user_rank = POP::exe('[@@:UM userrank][@@:QU find:all]',$query);			
	    foreach($user_rank as $value) {
		    $userrank[$value['userrank']['id']] = $value['userrank'];
	    }		
		$this->set('result',$result);
		$this->set('userrank',$userrank);
		
		$this->set('name',$name);		
		$this->set('email',$email);		
		$this->set('start_time',$start_time);		
		$this->set('end_time',$end_time);	
		$this->set('user_rank_id',$user_rank_id);	
		$this->set('user_status',$user_status);		
	}		
	
	public function locked() {	
        $user_id=Request::get_args('user_id');	
        if(!empty($user_id)){
	        //$user_id=$user_id;
        }else{
	        $user_id = intval(Request::get_args('id'));
        }
        if(!$user_id) { trigger_error("非法操作");}
		
		$query  = array();		
		$query['keyid'] = true;
		$safeData['user'] = array('locked' =>'1');				
		$query['set'] = $safeData;	
		$query['conditions'] = array('user.id'=>$user_id);						
		if(POP::exe('[@@:UM user][@@:QU save]',$query)) {		
			$array = array(
				'log_time' => date("Y-m-d H:i:s"),
				'admin_id' => $this->my->id,
			    'admin_name' => $this->my->username,
				'log_info' => "锁定用户",
			    'ip_address' => Request::getclientIp(),					
			);
			$safeData['adminlog'] = $array;								
			POP::exe('[@@:UM adminlog][@@:QU save]',$safeData);	
			$query  = array();		
			$query['keyid'] = true;
			$safeData['goods'] = array('is_on_sale' =>'0');				
			$query['set'] = $safeData;	
			$query['conditions'] = array('goods.shop_id'=>$user_id);			
			POP::exe('[@@:UM goods][@@:QU save]',$query);//将物品下架
	        $this->flash('修改成功！', Request::referer());
        } else {
	        $this->flash('修改失败！', Request::referer());
        }		
	}

	public function unlocked() {	
        $user_id=Request::get_args('user_id');	
        if(!empty($user_id)){
	        //$user_id=$user_id;
        }else{
	        $user_id = intval(Request::get_args('id'));
        }
        if(!$user_id) { trigger_error("非法操作");}
		
		$query  = array();		
		$query['keyid'] = true;
		$safeData['user'] = array('locked' =>'0');				
		$query['set'] = $safeData;	
		$query['conditions'] = array('user.id'=>$user_id);						
		if(POP::exe('[@@:UM user][@@:QU save]',$query)) {			
	        $this->flash('修改成功！', Router::url('admin/user/index'));
        } else {
	        $this->flash('修改失败！', Router::url('admin/user/index'));
        }		
	}	

	
	public function edit($id) {	
	    if(!empty($this->data)) {
		    $user_id = intval($this->data['user_id']);
            $array = array();
            if(isset($this->data['password'])&&!empty($this->data['password'])) {
	            $this->data['password'] = $this->C_auth->password($this->data['password']);
            } else if( isset($this->data['password']) && empty($this->data['password'])) {
			    unset($this->data['password']);			
				$query = array(
					'conditions' => array('user.id' => $id), 
					'fields' => array('user.*'), 
				);
				$rs = POP::exe('[@@:UM user][@@:QU find:first]',$query);				
				if($rs){
					$rs = $rs['user'];			
					$nowtime = date("Y-m-d H:i:s");
					if(intval($this->data['rank_id'])) {
						if($rs['rank_id']!=$this->data['rank_id']){
							$array = array(
								'user_id' => $user_id,
								'remind_info' => "在".$nowtime."，"."您的会员级别被修改。",
								'remind_time' => $nowtime,
							);
							$safedata['remindinfo'] = $array;								
							POP::exe('[@@:UM remindinfo][@@:QU save]',$safedata);

						}
					}
					if(intval($this->data['groupid'])) {
						if($rs['groupid']!=$this->data['groupid']){
							$array = array(
								'user_id' => $user_id,
								'remind_info' => "在".$nowtime."，"."您的会员权限级别被修改。",
								'remind_time' => $nowtime,
							);
							$safedata['remindinfo'] = $array;								
							POP::exe('[@@:UM remindinfo][@@:QU save]',$safedata);

						}
					}				
					if(isset($this->data['password'])&&!empty($this->data['password'])) {
						if($rs['password']!=$this->data['password']){
							$array = array(
								'user_id' => $user_id,
								'remind_info' => "在".$nowtime."，"."您的密码被修改。",
								'remind_time' => $nowtime,
							);
							$safedata['remindinfo'] = $array;								
							POP::exe('[@@:UM remindinfo][@@:QU save]',$safedata);	
						}
					}
				}	
				unset($this->data['user_id']);
				$safedata['user'] = $this->data;
				$query  = array();		
				$query['keyid'] = $user_id;				
				$query['set'] = $safedata;		
				if(POP::exe('[@@:UM user][@@:QU save]',$query)) {
					$this->flash('修改成功！', Router::url('admin/user/index'));
				}else {			
					$this->flash('修改失败！', Router::url('admin/user/edit/'.$user_id));
				}
            }				
		}else {
			$query = array(
				'conditions' => array('user.id' => $id), 
				'fields' => array('user.*'), 
			);
			$user_info = POP::exe('[@@:UM user][@@:QU find:first]',$query);			
			$user_info = $user_info['user'];
			$userrank =array();
			
			$query = array(
				'order' => ' id asc ', 
				'fields' => array('userrank.*'), 
			);
			$user_rank = POP::exe('[@@:UM userrank][@@:QU find:all]',$query);						
			foreach($user_rank as $value) {
				$userrank[$value['userrank']['id']] = $value['userrank'];
			}		
			$this->set('user_info',$user_info);
			$this->set('userrank',$userrank);		
        }		
    }	
	
	public function money($id) {	
	    if(!empty($this->data)) {
		    $user_id = intval($this->data['user_id']);
			$money = intval($this->data['money']);
            $array = array();
			$query = array(
				'conditions' => array('user.id' => $id), 
				'fields' => array('user.*'), 
			);
			$rs = POP::exe('[@@:UM user][@@:QU find:first]',$query);
			
            if($rs){
			    $rs = $rs['user'];			
	            $nowtime = date("Y-m-d H:i:s");
			    $array = array(
				    'user_id' => $user_id,
				    'remind_info' => "在".$nowtime."，管理员".$this->my->username . "给您的账号余额是".$rs['money']."人民币,现被充值".$money."人民币。",
				    'remind_time' => $nowtime,
			    );
				$safedata['remindinfo'] = $array;								
				POP::exe('[@@:UM remindinfo][@@:QU save]',$safedata);				
			    unset($safedata);			
                $safedata['user']['money'] = $money+$rs['money'];
				
				$query  = array();		
				$query['keyid'] = $user_id;			
				$query['set'] = $safedata;		
				if(POP::exe('[@@:UM user][@@:QU save]',$query)) {				
                    $data['usermoney']=array(
				        'user_id' => $user_id,
				        'type' => '0',
				        'name' => '后台管理员帮充值',
				        'intro' => '后台管理员帮充值,充值金额为'.$money.'元',
				        'money' => $money,
				        'time' => $nowtime,
		            );												
					POP::exe('[@@:UM usermoney][@@:QU save]',$data);	
					
			        $array = array(
				        'log_time' => date("Y-m-d H:i:s"),
				        'admin_id' => $this->my->id,
			            'admin_name' => $this->my->username,
				        'log_info' => "给账号为".$rs['username']."充值".$money."元",
			            'ip_address' => Request::getclientIp(),					
			        );
					$safeData['adminlog'] = $array;								
					POP::exe('[@@:UM adminlog][@@:QU save]',$safeData);						
                    $this->flash('充值成功！', Router::url('admin/user/index'));
                }else {			
		            $this->flash('充值失败！', Router::url('admin/user/edit/'.$user_id));
		        }				
            }else {
                $this->flash('该用户不存在！', Router::url('admin/user/index'));
            }			
		
		}else {
			$query = array(
				'conditions' => array('user.id' => $id), 
				'fields' => array('user.*'), 
			);
			$user_info = POP::exe('[@@:UM user][@@:QU find:first]',$query);		
			$user_info = $user_info['user'];	
		    $this->set('user_info',$user_info);		
        }		
    }	
	

	public function deluser() {	
        /* get */
        $rankid=Request::get_args('rank_id');
        if(is_array($rankid)){
	        $id=$rankid;
        }else{
	        $id = intval(Request::get_args('id'));
        }
        if(!$id) {
	        $this->flash('非法操作！', Router::url('admin/userrank/index'));
        } else {
			$query1 = array('conditions'=>array('userrank.id' => $id));	
			$query  = array();		
			$query['keyid'] = true;
			$safeData['user'] = array('rank_id' =>'1');				
			$query['set'] = $safeData;	
			$query['conditions'] = array('user.id'=>$id);						
			if(POP::exe('[@@:UM userrank][@@:QU del]',$query1) and POP::exe('[@@:UM user][@@:QU save]',$query)) {		
                $this->flash('操作成功！', Router::url('admin/userrank/index'));
            } else {
                $this->flash('操作失败！', Router::url('admin/userrank/index'));
            }
        }		
	}

	public function checkuseremail() {
        header('Content-Type: text/html; charset='.Param::read('App.encoding'));		
	    $v = Request::get_args("v");
		$query = array(
			'conditions' => array('user.email' => $v), 
			'fields' => array('user.*'), 
		);
		$id = POP::exe('[@@:UM user][@@:QU find:first]',$query);				
	    if ($id>0) {
		    echo 0;
	    }else{
		    echo 1;
	    }		
		exit;
	}	
	

}
?> 